﻿

/// <reference path="jquery-1.4.1.js" />


/*
    This is Javascript Utility Class using for common manipulate 

     Author : Tung.Ly
     Modified Date : 2013Jul04
*/
var JS_Utils = new function () {    
    //accessor variable
    this.addDays = fnAddDays;
    this.changeStringToDate = fnChangeStringToDate;
    this.getIdValue = fnGetIdValue;
    this.setIdValue = fnSetIdValue;
    this.isNullOrEmpty = fnIsNullOrEmpty;
    this.isNumber = fnIsNumber;
    this.isString = fnIsString;
    this.stringToJson = fnStringToJson;
    this.getHourMinute = fnToHourMinute;
    this.empty = fnStringEmpty;
    this.alert = fnAlert;
    this.confirm = fnConfirm;
    this.convertToString = fnConvertToString;
    this.isJsonFormat = fnIsJsonFormat;

    function fnIsJsonFormat(jsonString) {
        /// <summary>check string is Json format</summary>
        /// <param name="jsonString" type="String">The string of Json</param>                
        /// <returns type="Boolean"></returns>
        try {
            var val = eval('(' + jsonString + ')');
            return true;
        } catch (e) {
            return false;
        }
    }

    function fnConvertToString(objectValue) {
        /// <summary> Convert different objects to string:.</summary>
        /// <param name="objectValue" type="Object">object Type as :Boolean , Date, Number ,String,Array</param>        
        /// <returns type="String"></returns>
        return String(objectValue);
    }
        
    function fnAddDays(currentDate, numberOfDays) {
        /// <summary> Add number of days into specify Date  .</summary>
        /// <param name="currentDate" type="Date"></param>
        /// <param name="numberOfDays" type="Number"></param>
        /// <returns type="Date">return a new date was added amount of days</returns>

        try {
            currentDate.setDate(currentDate.getDate() + eval(numberDay));
            return currentDate;
        } catch (e) {
            alert("function addDays :" + e);
        }

        return new Date(1900, 0);
    }
    
    function fnChangeStringToDate(strDate) {
        /// <summary> change string with pattern (dd/MM/yyyy) to Date.</summary>
        /// <param name="strDate" type="String"></param>        
        /// <returns type="Date">return a date</returns>


        var arrDate = strDate.split("/");

        if (arrDate.length == 3) {
            //lay ngay , thang , nam
            var day = eval(arrDate[0]);
            var month = eval(arrDate[1]);
            var year = eval(arrDate[2]);

            //lấy ngày
            var dateNew = new Date(year, month - 1, day);
            return dateNew;
        }
        else {
            return new Date(1900, 1, 1);
        }
    

    }
    
    function fnToHourMinute(date) {
        /// <summary> get HourMinute with pattern (HH:mm)</summary>
        /// <param name="date" type="Date">input the date </param>        
        /// <returns type="String">return HourMinute</returns>

        var hourMinute = "";
        var hour = 0;
        var minute = 0;

        if (date instanceof Date) {
            hour = date.getHours();
            minute = date.getMinutes();
            hour > 9 ? hourMinute = hour.toString() : hourMinute = "0" + hour.toString();
            hourMinute += ":";
            minute > 9 ? hourMinute += minute.toString() : hourMinute += "0" + minute.toString();
        }
        else {
            hourMinute = "00:00";
        }

        return hourMinute;
    }

    function fnGetIdValue(id) {
        /// <summary> get getIdValue.</summary>
        /// <param name="id" type="String">Get Input Id in html tag</param>        
        /// <returns type="String">return IdValue</returns>


        if (fnIsNullOrEmpty(document.getElementById(id))) {
            return "";
        }

        var idValue = document.getElementById(id).value;
       
        return idValue;
    }


    function fnSetIdValue(id, idValue) {
        /// <summary>set IdValue by id.</summary>
        /// <param name="id" type="String">Get Input Id in html tag</param>        
        /// <param name="idValue" type="String">Input value of Id need set value</param>        
        /// <returns type="Bool">return true if success // return false if failure</returns>
                
        if (fnIsNullOrEmpty(document.getElementById(id))) {
            return false;
        }    
        document.getElementById(id).value = idValue;    
        return true;
    }
    
    function fnIsNullOrEmpty(value) {
        /// <summary>Check value is Null or Empty.</summary>
        /// <param name="value" type="String"></param>                
        /// <returns type="Bool"></returns>

        var isNullorEmpty = false;
        switch(value) {
            case "": isNullorEmpty = true; break;
            case null: isNullorEmpty = true; break;            
            case undefined:isNullorEmpty = true; break;
            default: isNullorEmpty = false;
        }
        return isNullorEmpty;
    }
       
    function fnStringToJson(jsonString) {
        /// <summary>Convert String to Json format</summary>
        /// <param name="jsonString" type="String">The string of Json</param>                
        /// <returns type="Json"></returns>
        try {
            var val = eval('(' + jsonString + ')');
            return val;
        } catch (e) {
            alert("function StringToJson : "+e);
        }
        
        
    }

    function fnIsNumber(value) {
        /// <summary>Check value input is Number type or not</summary>
        /// <param name="value" type="String">The value want to check</param>                
        /// <returns type="bool"></returns>

        var _isNumber = false;
        if(!isNaN(value))
        {
            _isNumber = true;
        }
        return _isNumber;
    }

    function fnIsString(value) {
        /// <summary>Check value input is String type or not</summary>
        /// <param name="value" type="String">The value want to check</param>                
        /// <returns type="bool"></returns>

        return !fnIsNumber(value);
    }
    
    function fnStringEmpty() {
        /// <summary>Trả về chuổi rổng</summary>        
        /// <returns type="String"></returns>
        return "";
    }

    function fnAlert(alertMessage) {
        /// <summary>Display alert Dialog Box</summary>                
        /// <param name="alertMessage" type="String">The value want to alert</param>                
        window.alert(alertMessage);
    }

    function fnConfirm(confirmMessage) {
        /// <summary>Display Confirmation Dialog Box</summary>                
        /// <param name="confirmMessage" type="String">The value want to inform to user</param>                
        /// <returns type="bool"></returns>
        return window.confirm(confirmMessage);
    }

    

}//end Utility class


/*
All Validation of javascript

 
Author : Tung.Ly
Modified Date : 2013Jul04
*/
var JS_Validations = new function () {
    

}



/*
    All Constants of javascript

 
     Author : Tung.Ly
     Modified Date : 2013Jul04
*/
var JS_Constants = new function () {
    this.PersonalInfo = "PersonalInfo";   

}

